package com.pick.admin.controller;

import com.pick.admin.dto.LoginRequest;
import com.pick.admin.dto.LoginResponse;
import com.pick.admin.dto.RegisterRequest;
import com.pick.admin.dto.RegisterResponse;
import com.pick.admin.dto.UserInfoResponse;
import com.pick.admin.entity.SysMenu;
import com.pick.admin.entity.SysUser;
import com.pick.admin.service.AuthService;
import jakarta.validation.Valid;
import com.pick.base.annotation.OperationLog;
import com.pick.base.annotation.RateLimit;
import com.pick.base.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 认证控制器
 */
@Tag(name = "认证管理")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Operation(summary = "用户登录")
    @OperationLog(module = "认证管理", type = "登录", description = "用户登录")
    @RateLimit(window = 60, limit = 5, type = RateLimit.LimitType.IP, message = "登录过于频繁，请1分钟后再试")
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        LoginResponse response = authService.login(request.getUsername(), request.getPassword());
        return Result.success("登录成功", response);
    }

    @Operation(summary = "用户登出")
    @OperationLog(module = "认证管理", type = "登出", description = "用户登出")
    @RateLimit(window = 60, limit = 10, type = RateLimit.LimitType.USER, message = "操作过于频繁，请稍后再试")
    @PostMapping("/logout")
    public Result<String> logout() {
        authService.logout();
        return Result.success("登出成功");
    }
    
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<RegisterResponse> register(@Valid @RequestBody RegisterRequest request) {
        // 验证确认密码
        if (!request.getPassword().equals(request.getConfirmPassword())) {
            return Result.error("两次输入的密码不一致");
        }
        
        // 调用注册服务
        SysUser user = authService.register(
            request.getUsername(),
            request.getPassword(),
            request.getRealName(),
            request.getEmail(),
            request.getMobile()
        );
        
        // 构建响应
        RegisterResponse response = new RegisterResponse(
            user.getId(),
            user.getUsername(),
            user.getRealName(),
            user.getEmail(),
            user.getMobile(),
            user.getStatus(),
            user.getCreateTime()
        );
        
        return Result.success(response);
    }

    @Operation(summary = "获取当前用户信息")
    @RateLimit(window = 60, limit = 30, type = RateLimit.LimitType.USER, message = "请求过于频繁，请稍后再试")
    @GetMapping("/userinfo")
    public Result<UserInfoResponse> getUserInfo() {
        SysUser user = authService.getCurrentUser();
        List<String> roles = authService.getCurrentUserRoles();
        List<String> permissions = authService.getCurrentUserPermissions();
        
        UserInfoResponse response = new UserInfoResponse();
        response.setId(user.getId());
        response.setUsername(user.getUsername());
        response.setRealName(user.getRealName());
        response.setEmail(user.getEmail());
        response.setMobile(user.getMobile());
        response.setStatus(user.getStatus());
        response.setDeptId(user.getDeptId());
        response.setLastLoginTime(user.getLastLoginTime());
        response.setCreateTime(user.getCreateTime());
        response.setRoles(roles);
        response.setPermissions(permissions);
        
        return Result.success(response);
    }

    @Operation(summary = "获取当前用户菜单")
    @RateLimit(window = 60, limit = 20, type = RateLimit.LimitType.USER, message = "请求过于频繁，请稍后再试")
    @GetMapping("/menus")
    public Result<List<SysMenu>> getUserMenus() {
        List<SysMenu> menus = authService.getCurrentUserMenus();
        return Result.success(menus);
    }

    }